//
//  ZCSListenNewsRecommendViewController.m
//  聆听FM
//
//  Created by dllo on 16/6/8.
//  Copyright © 2016年 Dark. All rights reserved.
//

#import "ZCSListenNewsRecommendViewController.h"
#import "ZCSTipsReTableViewCell.h"
#import "ZCSReHeaderTableViewCell.h"
#import "ZCSNewsCateListModel.h"
#import "ZCSNewsCateListListModel.h"
#import "DCPicScrollView.h"
#import "ZCSListenRankTableViewCell.h"
#import "ZCSFocusImagesModel.h"
#import "AlbumDetailViewController.h"
#import "ZCSHearTableViewCell.h"
#import "ZCSBoutiqueListenViewController.h"
#import "ZCSTipsRecomdViewController.h"
#import "YWSquarePhotoViewController.h"
#import "ZCSListenListDetailViewController.h"
#import "ZCSPlayMusicViewController.h"
#import "CBStoreHouseRefreshControl.h"
@interface ZCSListenNewsRecommendViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) NSMutableArray *dataArray;
@property (nonatomic, retain) DCPicScrollView *picView;
@property (nonatomic, strong) CBStoreHouseRefreshControl *storeHouseRefreshControl;
@property (nonatomic, retain) ZCSNewsCateListListModel *pushModel;
@end

@implementation ZCSListenNewsRecommendViewController
- (void)dealloc
{
    self.tableView.dataSource = nil;
    self.tableView.delegate = nil;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    if ([self respondsToSelector:@selector(edgesForExtendedLayout)]) {
        self.edgesForExtendedLayout = UIRectEdgeNone;
    }
    self.view.backgroundColor = [UIColor redColor];
    self.dataArray = [NSMutableArray array];
    [self creatView];
    [self getDataWith:self.cId];
}

- (void)creatView
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREENWIDTH, SCREENHEIGHT - 94 * FITHEIGHT) style:UITableViewStyleGrouped];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.separatorStyle = 0;
    self.tableView.sectionFooterHeight = 10 * FITHEIGHT;
    [self.view addSubview:_tableView];
    
    UIView *foot = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREENWIDTH, 70 * FITHEIGHT)];
    self.tableView.tableFooterView = foot;
    
    [self.tableView registerNib:[UINib nibWithNibName:@"ZCSTipsReTableViewCell" bundle:nil] forCellReuseIdentifier:@"TipCell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"ZCSReHeaderTableViewCell" bundle:nil] forCellReuseIdentifier:@"reHeader"];
    [self.tableView registerNib:[UINib nibWithNibName:@"ZCSListenRankTableViewCell" bundle:nil] forCellReuseIdentifier:@"rankCell"];

    [self.tableView registerNib:[UINib nibWithNibName:@"ZCSHearTableViewCell" bundle:nil] forCellReuseIdentifier:@"hearTVC"];
    //下拉刷新
    
//    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
//        
//        [self getData];
//    }];
    
    self.storeHouseRefreshControl = [CBStoreHouseRefreshControl attachToScrollView:self.tableView target:self refreshAction:@selector(refreshTriggered:) plist:@"LTFM" color:[UIColor blackColor] lineWidth:3 dropHeight:80 scale:1.2 horizontalRandomness:300 reverseLoadingAnimation:NO internalAnimationFactor:0.7];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    [self.storeHouseRefreshControl scrollViewDidScrollWithType:0];
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    [self.storeHouseRefreshControl scrollViewDidEndDragging];
}

- (void)refreshTriggered:(id)sender
{
    
    [self performSelector:@selector(finishRefreshControl) withObject:nil afterDelay:2 inModes:@[NSRunLoopCommonModes]];
}

- (void)finishRefreshControl
{
    [self getData];
}

- (void)getData
{
    [self.dataArray removeAllObjects];
    [self.tableView reloadData];
    [self getDataWith:self.cId];
}
- (void)creatHeaderViewWith:(NSArray *)images ZCSNewsCateListListModel:(ZCSNewsCateListListModel *)model
{
    self.pushModel = model;
    UIView *header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREENWIDTH, 270 * FITHEIGHT)];
    self.picView = [DCPicScrollView picScrollViewWithFrame:CGRectMake(0, 0, SCREENWIDTH, 200 * FITHEIGHT) WithImageUrls:images];
    self.picView.placeImage = [UIImage sd_animatedGIFNamed:@"占位图"];
    __weak ZCSListenNewsRecommendViewController *weatSelf = self;
    [self.picView setImageViewDidTapAtIndex:^(NSInteger index) {
        ZCSFocusImagesModel *model = [weatSelf.newsModel.focuImageArray objectAtIndex:index];
        if (model.type == 2) {
            AlbumDetailViewController *advc = [[AlbumDetailViewController alloc] init];
            advc.albmId = model.albumId;
            advc.hidesBottomBarWhenPushed = YES;
            [weatSelf.navigationController pushViewController:advc animated:YES];
        }
        //听单
        if (model.type == 9) {
            ZCSListenListDetailViewController *list = [[ZCSListenListDetailViewController alloc] init];
            list.ID = model.specialId;
            list.type = [NSString stringWithFormat:@"%ld", model.subType];
            weatSelf.hidesBottomBarWhenPushed = YES;
            [weatSelf.navigationController pushViewController:list animated:YES];
        }
        //歌单
        if (model.type == 3) {
            NSMutableArray *tempArray = [NSMutableArray array];
            ZCSListenListListModel *playModel = [[ZCSListenListListModel alloc] init];
            playModel.id = model.trackId;
            playModel.title = model.longTitle;
            [tempArray addObject:playModel];
            ZCSPlayMusicViewController *pmvc = [ZCSPlayMusicViewController sharePlayVC];
            pmvc.dataArray = tempArray;
            pmvc.maxPage = 1;
            pmvc.musicIndex = 0;
            [weatSelf presentViewController:pmvc animated:YES completion:nil];
        }
    }];
    self.picView.AutoScrollDelay = 3.0;
    [header addSubview:_picView];
    ZCSListenRankTableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"rankCell"];
    cell.frame = CGRectMake(0, 200 * FITHEIGHT, SCREENWIDTH, 60 * FITHEIGHT);
    cell.listListModel = model;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(rankCellAction:)];
    [cell addGestureRecognizer:tap];
    [header addSubview:cell];
    self.tableView.tableHeaderView = header;
}
- (void)rankCellAction:(UITapGestureRecognizer *)tap
{
    YWSquarePhotoViewController *spvc = [[YWSquarePhotoViewController alloc] init];
    spvc.titleString = self.pushModel.title;
    spvc.key = self.pushModel.key;
    spvc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:spvc animated:YES];
}
- (void)creatHeaderViewWith:(NSArray *)images
{
    UIView *header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREENWIDTH, 200 * FITHEIGHT)];
    self.picView = [DCPicScrollView picScrollViewWithFrame:CGRectMake(0, 0, SCREENWIDTH, 200 * FITHEIGHT) WithImageUrls:images];
    self.picView.placeImage = [UIImage sd_animatedGIFNamed:@"占位图"];
    __weak ZCSListenNewsRecommendViewController *weatSelf = self;
    [self.picView setImageViewDidTapAtIndex:^(NSInteger index) {
        ZCSFocusImagesModel *model = [weatSelf.newsModel.focuImageArray objectAtIndex:index];
        if (model.type == 2) {
            AlbumDetailViewController *advc = [[AlbumDetailViewController alloc] init];
            advc.albmId = model.albumId;
            advc.hidesBottomBarWhenPushed = YES;
            [weatSelf.navigationController pushViewController:advc animated:YES];
        }
        //听单
        if (model.type == 9) {
            ZCSListenListDetailViewController *list = [[ZCSListenListDetailViewController alloc] init];
            list.ID = model.specialId;
            list.type = [NSString stringWithFormat:@"%ld", model.subType];
            weatSelf.hidesBottomBarWhenPushed = YES;
            [weatSelf.navigationController pushViewController:list animated:YES];
        }
        //歌单
        if (model.type == 3) {
            NSMutableArray *tempArray = [NSMutableArray array];
            ZCSListenListListModel *playModel = [[ZCSListenListListModel alloc] init];
            playModel.id = model.trackId;
            playModel.title = model.longTitle;
            [tempArray addObject:playModel];
            ZCSPlayMusicViewController *pmvc = [ZCSPlayMusicViewController sharePlayVC];
            pmvc.dataArray = tempArray;
            pmvc.maxPage = 1;
            pmvc.musicIndex = 0;
            [weatSelf presentViewController:pmvc animated:YES completion:nil];
        }

    }];
    self.picView.AutoScrollDelay = 3.0;
    [header addSubview:_picView];
    self.tableView.tableHeaderView = header;
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if (self.dataArray.count != 0) {
        return self.dataArray.count;
    }
    return 1;
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (self.dataArray.count != 0) {
        ZCSNewsCateListModel *model = [self.dataArray objectAtIndex:section];
        return model.list.count;
    }
    return 0;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.dataArray.count != 0) {
        ZCSNewsCateListModel *sectionModel = [self.dataArray objectAtIndex:indexPath.section];
        ZCSNewsCateListListModel *model = [[[self.dataArray objectAtIndex:indexPath.section] list] objectAtIndex:indexPath.row];
        if (sectionModel.moduleType != 4) {
            ZCSTipsReTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TipCell"];
            
            cell.newsListModel = model;
            
            return cell;
        }
        else
        {
            
            ZCSHearTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"hearTVC"];
            cell.newsListModel = model;
            return cell;
        }
    }
    ZCSTipsReTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TipCell"];

    return cell;

    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    ZCSNewsCateListModel *sectionModel = [self.dataArray objectAtIndex:indexPath.section];
    ZCSNewsCateListListModel *model = [[[self.dataArray objectAtIndex:indexPath.section] list] objectAtIndex:indexPath.row];
    if (sectionModel.moduleType != 4) {
        AlbumDetailViewController *advc = [[AlbumDetailViewController alloc] init];
        advc.albmId = model.albumId;
        advc.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:advc animated:YES];
    }
    else
    {
        ZCSListenListDetailViewController *lldvc = [[ZCSListenListDetailViewController alloc] init];
        lldvc.ID = model.specialId;
       
        lldvc.type = [NSString stringWithFormat:@"%@", model.contentType];
        lldvc.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:lldvc animated:YES];
    }
    
    
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 40 * FITHEIGHT;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 80 * FITHEIGHT;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (self.dataArray.count != 0) {
        ZCSReHeaderTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reHeader"];
        cell.backgroundColor = [UIColor whiteColor];
        ZCSNewsCateListModel *model = [self.dataArray objectAtIndex:section];
        cell.headerLabel.text = model.title;
        cell.headerLabel.textColor = [UIColor blackColor];
        cell.moreLabel.userInteractionEnabled = YES;
        cell.moreLabel.tag = 1000 + section;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(moreLabelAction:)];
        [cell.moreLabel addGestureRecognizer:tap];
        
        return cell;
    }
    ZCSReHeaderTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reHeader"];
    return cell;
    
}
- (void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBar.hidden = NO;
}
- (void)viewWillDisappear:(BOOL)animated
{
    self.navigationController.navigationBar.hidden = NO;
}

#pragma mark -- 更多按钮
- (void)moreLabelAction:(UITapGestureRecognizer *)tap
{
    NSInteger num = tap.view.tag - 1000;
    ZCSNewsCateListModel *model = [self.dataArray objectAtIndex:num];
    if (model.moduleType == 4) {
        ZCSBoutiqueListenViewController *blvc = [[ZCSBoutiqueListenViewController alloc] init];
        blvc.caId = self.cId;
        blvc.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:blvc animated:YES];
    }
    else
    {
        ZCSTipsRecomdViewController *trvc = [[ZCSTipsRecomdViewController alloc] init];
        trvc.titleString = model.title;
        trvc.cId = self.cId;
        trvc.keywordId = model.keywordId;
        trvc.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:trvc animated:YES];
    }
}
#pragma mark -- 推荐数据源
- (void)getDataWith:(NSInteger)cateId
{
    NSString *string = [NSString stringWithFormat:@"http://mobile.ximalaya.com/mobile/discovery/v2/category/recommends?categoryId=%ld&contentType=album&device=android&version=4.3.92", cateId];
    __weak ZCSListenNewsRecommendViewController *nvc = self;
    [LTNetTool GetNetWithURL:string Body:nil Header:nil Response:0 Success:^(id result) {
        nvc.newsModel = [[ZCSListenNewsModel alloc] initWithDic:result];
        ZCSNewsCateModel *cateModel = self.newsModel.cateModel;
        NSArray *tempArray = cateModel.list;
        for (ZCSNewsCateListModel *cateListModel in tempArray) {
            if (cateListModel.list.count != 0) {
                if (cateListModel.moduleType != 2) {
                    [nvc.dataArray addObject:cateListModel];
                }
            }
        }
       
        NSMutableArray *scrollArray = [NSMutableArray array];
        for (ZCSFocusImagesModel *model in nvc.newsModel.focuImageArray) {
            if (model.pic) {
                [scrollArray addObject:model.pic];

            }
        }
        
        ZCSNewsCateListModel *rankModel = tempArray[0];
        
        if (scrollArray.count != 0) {
            if (rankModel.moduleType == 2) {
                [nvc creatHeaderViewWith:scrollArray ZCSNewsCateListListModel:rankModel.list[0]];
            }
            else
            {
                [nvc creatHeaderViewWith:scrollArray];
            }
           
        }
        
        [nvc.tableView reloadData];
        [nvc.storeHouseRefreshControl finishingLoading];
        
        
    } Failure:^(NSError *error) {
        [nvc.storeHouseRefreshControl finishingLoading];
        NSLog(@"%@", error);
    }];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
